package com.videos.service.impl;

import com.alibaba.fastjson.JSON;
import com.commons.entity.MovieUserInfo;
import com.commons.enums.ResponseCodeEnum;
import com.commons.po.MovieInfoPo;
import com.commons.utils.Constance;
import com.commons.utils.ResponseResult;
import com.commons.vo.QueryMovieInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.videos.dao.MovieMapper;
import com.videos.entity.HistoryMoviePo;
import com.videos.service.MovieService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author mis_wu
 * @date 2022/2/27 17:19
 */
@Transactional
@Service
@Slf4j
public class MovieServiceImpl implements MovieService {

    @Resource
    private MovieMapper movieMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 分页查询电影列表
     * @param queryMovieInfoVo
     * @return
     */
    @Override
    public PageInfo<MovieInfoPo> queryMovieInfoByPage(QueryMovieInfoVo queryMovieInfoVo) {
        PageHelper.startPage(queryMovieInfoVo.getPage(),queryMovieInfoVo.getPageSize());
        List<MovieInfoPo> movieList = movieMapper.queryMovieInfoByPage(queryMovieInfoVo);
        return new PageInfo<MovieInfoPo>(movieList);
    }

    /**
     * 查询免费的电影 游客登录查询
     * @param queryMovieInfoVo
     * @return
     */
    @Override
    public List<MovieInfoPo> queryCommonsMovies(QueryMovieInfoVo queryMovieInfoVo) {
        //查询非vip总数
        Integer totals = queryFreeMovieTotals();
        //随机取10-20 待优化
        Integer low = (int)(10+Math.random()*(20-10+1));
        Integer heigh=low + 50;
        List<MovieInfoPo> movieList = null;
        if (low <= totals && heigh <= totals){
            //随机取50条
            movieList = movieMapper.queryCommonsMovies(low,heigh,queryMovieInfoVo.getMovieName(),queryMovieInfoVo.getMovieType());
        }else {
            //固定取100条
            movieList= movieMapper.queryCommonsMoviesOther(queryMovieInfoVo.getMovieName(),queryMovieInfoVo.getMovieType());
        }
        return movieList;
    }

    /**
     * 查询免费的电影总数
     * @return
     */
    public Integer queryFreeMovieTotals(){
        return movieMapper.queryFreeMovieTotals();
    }

    /**
     * 根据userId查询搜索记录
     * @param userId
     * @return
     */
    @Override
    public List<HistoryMoviePo> queryHistoryMovie(Long userId) {
        return movieMapper.queryHistoryMovie(userId);
    }

    /**
     * 根据搜索关键字查询 name
     * @param page
     * @param size
     * @param searchKey
     * @return
     */
    @Override
    public PageInfo<MovieInfoPo> queryMoviesBySearchKey(int page,int size,String searchKey) {
        PageHelper.startPage(page,size);
        List<MovieInfoPo> movieInfoPos = movieMapper.queryMoviesBySearchKey(searchKey);
        return new PageInfo<>(movieInfoPos);
    }
}
